﻿using Lin_shop.Models.Database;
using Lin_shop.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Security.Claims;

namespace LDC_Shop.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarController : ControllerBase
    {
        private readonly ShopContext _db;
        private readonly ICarService _carService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public CarController(ShopContext db, ICarService carService)
        {
            _db = db;
            _carService = carService;
        }
        //ddd
        //加入购物车接口（需授权） 输入：商品Id 输出：加入消息
        /// <summary>
        /// 加入购物车接口
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string AddCar(int goodId)
        {
            //实现加入购物车接口

            //1、登录用户购物车没有该商品
            var userId = Convert.ToInt32(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            //获取用户的购物车对象
            var car = _carService.GetCarByUId(goodId, userId);
            var result = false;
            if (car == null)
                //添加
                result = _carService.AddCar(goodId, userId);
            else
                //更新
                result = _carService.UpdateCar(car, 1);
            //调用加入购物车服务
            if (result)
                return "加入成功";
            else
                return "加入失败";
        }

        //减少购物车接口（需授权） 输入：商品Id 输出：减少消息
        /// <summary>
        /// 减少购物车接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string UpdateCar(int goodId)
        {
            //1、购物车有这个商品
            var userId = Convert.ToInt32(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            //获取购物车商品
            var car = _carService.GetCarByUId(goodId, userId);
            var result = false;
            if (car != null)
            {
                //1、购物车商品数量为1 再减少 0 需要删除这个商品
                if (car.Count == 1)
                    result = _carService.DeleteCar(car);
                //2、不为1 直接减少 更新
                else
                    result = _carService.UpdateCar(car, -1);
                if (result)
                    return "更新成功";
                else
                    return "更新失败";
            }
            else
            {
                //2、购物车没有这商品 返回消息
                return "商品不存在";
            }

        }
        //删除购物车商品接口（需授权） 输入：商品Id 输出：删除消息
        /// <summary>
        /// 删除购物车商品接口
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public string DelCar(int goodId)
        {
            //实现删除购物车商品
            var userId = Convert.ToInt32(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            //获取购物车商品
            var car = _carService.GetCarByUId(goodId, userId);
            if (car == null)
                return "商品不存在";
            //删除购物车商品
            var result = _carService.DeleteCar(car);
            if (result)
                return "删除成功";
            return "删除失败";
        }


        //查询购物车商品接口（需授权） 输入：无 输出：购物车商品列表（商品标题、商品图片、商品Id,购物车商品数量）购物车价格总额
        /// <summary>
        /// 根据登录用户查询购物车商品记录
        /// </summary>
        [HttpPost]
        [Authorize]
        public GetCarsResponse GetCars()
        {
            //实现购物车列表
            //上下.表名.操作
            //登录用户的购物车数据
            var userId = Convert.ToInt32(Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value);
            //获取购物车列表
            var list = _carService.GetCarList(userId);
            //单价 * 数量
            //遍历求和
            var price = list.Sum(x => x.Count * x.Price);
            var reponse = new GetCarsResponse
            {
                List = list,
                TotalPrice = price
            };
            return reponse;
        }



    }
}
